﻿<documents created="2010-12-15 11:36:08Z"><document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Controller\CAViewController.cs" renderPrefix="s0"><chunk hits="-1" domain="unknown"><source><![CDATA[/*
 * A CaViewController controls the messaging between views and the model. It subscribes to user events from the view and sends updates
 * on the evolvement of a Cellular Automaton. All user interaction with an IVIEW is handled and propogated to the right places. As all interaction
 * and modelling in the sysmtem goes through this class, it also defines the logic behind how the system reacts to system manipulation. Due to the
 * diffrences between the hardware and the gui some seperate logical commands to this class are needed, but in general their behavour is merged
 * together and abstracted in the same way.
 * Author: Anderes Høst | ahkj@itu.dk
 * Date: 04 Dec 2010
 */
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.IO;
using AutomaTones.HW_Sense_Compute;
using AutomaTones.Model.Automaton_Model;
using AutomaTones.Model.Automaton_Model.RuleLib;
using AutomaTones.Model.MIDI;
using AutomaTones.Model.MIDI.Mappers;
using AutomaTones.Model.Tools;
using AutomaTones.View;
using AutomaTones.View.Midi_Out_View;
using Microsoft.Pex.Framework;
using Midi;


namespace AutomaTones.Controller {
    [PexClass]
    partial class CAViewController : IController<IView> {

        private const int LAUNCHPAD_DIMENSION = 8;
        private const int NUMBER_OF_STATES = 7;
        private Channel _chosenChannel;
        private IHardwareView _hardWareView;
        private State[,] _grid, _rule;
        private List<State[,]> rules;
        private bool _userIsSettingCAInitialState, _userIsSettingCARule, _stateBeingChanged, _caReadyForReplay;

        //Cell manipulation
        private State _state = new State();
        private int _x, _y, validTimestamp;

        //Musical references 
        private MidiOut _mo;
        private Tempo _tempo;
        private IMapper _mapper = new BechTranslator(); // we need this for insitialization
        private ConnectVirtualMidi _vm;

        private CellularAutomaton _ca;
        private List<CellularAutomaton> _caHistory;

        public CAViewController() {
            rules = new List<State[,]>();
            _grid = new State[LAUNCHPAD_DIMENSION, LAUNCHPAD_DIMENSION];
            //for custom rule setting dimension are never biiger than 3 x 3
            _rule = new State[3, 3];
            try {
                _vm = new ConnectVirtualMidi();
            } catch (Exception ex) {
                //Midi not installed correctly and en exception is unavoidable. 
                //As graphical interaction is still possible we don't kill the system but let the user
                //continue to use it in a less functional way way. If critcal one could consider
                //the state of CaViewController invalid and exit here.
                Console.WriteLine(ex.Message);
            }
              _mo = new MidiOut(Channel.Channel1, _mapper);
              _tempo = MainController.Tempo;

            _caHistory = new List<CellularAutomaton>();
            ResetRuleGrid();
            ResetGrid();
        }

        public bool IsPLaying { get; set; }
        public bool IsPaused { get; set; }
        public int Volume { get; set; }
        public List<Channel> Channels { get; set; }

        /// <summary>
        /// Add an IView to this controller.
        /// </summary>
        /// <param name="view"></param>
        [PexMethod]
        public void AddView(IView view) ]]></source>
</chunk>
<method name="AddView" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000ec" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="2" total="2" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(view != null);
            Contract.Ensures(View == view);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[View = view;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0009" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Add an IHardwareView to this controller. If the inputmode is set to launchpad in the controller this
        /// view will be notified.
        /// </summary>
        /// <param name="hardWareView"></param>
        [PexMethod]
        public void AddHwView(IHardwareView hardWareView) ]]></source>
</chunk>
<method name="AddHwView" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000ed" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(hardWareView != null);
            Contract.Ensures(_hardWareView == hardWareView);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_hardWareView = hardWareView;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// This method should be invoked when a pad is pressed on the launchpad. 
        /// </summary>
        [PexMethod]
        public void LaunchpadClicked(int x, int y) ]]></source>
</chunk>
<method name="LaunchpadClicked" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000ee" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="3" total="5" coveredRatio="0.6"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (!OkToUseLaunchpad())]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="000b" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="000d" domain="userortestcode"><source><![CDATA[CellClicked(x, y);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0016" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Called whenever one of the statechange buttons on the right side of the Launchpad is pressed or released
        /// </summary>
        /// <param name="pushingStateButton">Indicates if a statechange button is being pressed</param>
        [PexMethod]
        public void StateButtonDown(bool pushingStateButton) ]]></source>
</chunk>
<method name="StateButtonDown" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000ef" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_stateBeingChanged = pushingStateButton;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Updates the state after a statechange button has been released
        /// </summary>
        /// <param name="state">The state change selected on the Launchpad</param>
        [PexMethod]
        public void State(State state) ]]></source>
</chunk>
<method name="State" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f0" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(state != null);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_state = state;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Method that reacts to an'Ok' sent from the implementer of IView. It reacts in response to
        /// what the user was doing - setting up a rule or CA.
        /// </summary>
        [PexMethod]
        public void Ok() ]]></source>
</chunk>
<method name="Ok" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f1" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="39" value="1"/>
<hit index="45" value="1"/>
<hit index="48" value="1"/>
<hit index="50" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="4" total="23" coveredRatio="0.17391304347826087"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_userIsSettingCAInitialState && (ChosenCAType == CAType.TwoDimensional ||
                                                 ChosenCAType == CAType.TwoDimensionalEXT || 
                                                 ChosenCAType == CAType.OneDimensional))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0031" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                //was setting initial state and is now done
                ]]></source>
</chunk>
<chunk hits="0" offset="0032" domain="userortestcode"><source><![CDATA[_userIsSettingCARule = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ //Now setting the rule
                ]]></source>
</chunk>
<chunk hits="0" offset="0039" domain="userortestcode"><source><![CDATA[_userIsSettingCAInitialState = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0040" domain="userortestcode"><source><![CDATA[ResetRuleGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0047" domain="userortestcode"><source><![CDATA[if (_hardWareView != null)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="0" offset="0054" domain="userortestcode"><source><![CDATA[_hardWareView.ClearGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0060" domain="userortestcode"><source><![CDATA[View.SystemMessage("Set rule # " + (rules.Count + 1) +
                                   " Press ok to enter more rules. Press Play to listen.");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="008d" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="0090" domain="userortestcode"><source><![CDATA[if (_userIsSettingCARule)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="009d" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="009e" domain="userortestcode"><source><![CDATA[SaveRule();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00a5" domain="userortestcode"><source><![CDATA[ResetRuleGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="00ac" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="00ad" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Play was sent from the user. Doesn't actually 'play music' but sets up the process of generating music
        /// based on CAs by gathering the input and choices of the user.
        /// </summary>
        [PexMethod]
        public void PlaySentFromUser() ]]></source>
</chunk>
<method name="PlaySentFromUser" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f2" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="16" value="1"/>
<hit index="17" value="1"/>
<hit index="27" value="1"/>
<hit index="28" value="1"/>
<hit index="30" value="1"/>
<hit index="31" value="1"/>
<hit index="46" value="1"/>
<hit index="47" value="1"/>
<hit index="62" value="1"/>
<hit index="63" value="1"/>
<hit index="64" value="1"/>
<hit index="65" value="1"/>
<hit index="66" value="1"/>
<hit index="76" value="1"/>
<hit index="77" value="1"/>
<hit index="78" value="1"/>
<hit index="79" value="1"/>
<hit index="80" value="1"/>
<hit index="81" value="1"/>
<hit index="82" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="14" total="33" coveredRatio="0.42424242424242425"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_caReadyForReplay)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="000e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="000f" domain="userortestcode"><source><![CDATA[IsPLaying = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0017" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            }
            ]]></source>
</chunk>
<chunk hits="1" offset="001c" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.GameOfLife)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="002c" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="002d" domain="userortestcode"><source><![CDATA[CaTypeSetup.SetupGameOfLife(ref _ca, _grid.GetLength(0), _grid.GetLength(1), 2, _grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0058" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="005e" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.GameOfLifeEXT)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="006e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="006f" domain="userortestcode"><source><![CDATA[CaTypeSetup.SetupGameOfLife(ref _ca, _grid.GetLength(0), _grid.GetLength(1), NUMBER_OF_STATES, _grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="009a" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="00a0" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.OneDimensional)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="00b0" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                //    SetUpOneDimensionalCA();
            ]]></source>
</chunk>
<chunk hits="0" offset="00b1" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="00b7" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.TwoDimensional)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="00c7" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00c8" domain="userortestcode"><source><![CDATA[SaveRule();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00cf" domain="userortestcode"><source><![CDATA[_userIsSettingCARule = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00d6" domain="userortestcode"><source><![CDATA[CaTypeSetup.SetupTwoDimensional(ref _ca, rules, 2, (uint)_grid.GetLength(0), _grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00fb" domain="userortestcode"><source><![CDATA[ResetGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0102" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="0105" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.TwoDimensionalEXT)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0115" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0116" domain="userortestcode"><source><![CDATA[SaveRule();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="011d" domain="userortestcode"><source><![CDATA[_userIsSettingCARule = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0124" domain="userortestcode"><source><![CDATA[CaTypeSetup.SetupTwoDimensional(ref _ca, rules, 8, (uint)_grid.GetLength(0), _grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0149" domain="userortestcode"><source><![CDATA[ResetGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0150" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[


            ]]></source>
</chunk>
<chunk hits="1" offset="0151" domain="userortestcode"><source><![CDATA[_userIsSettingCARule = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0158" domain="userortestcode"><source><![CDATA[IsPaused = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0160" domain="userortestcode"><source><![CDATA[if (_ca != null)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="016d" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="016e" domain="userortestcode"><source><![CDATA[IsPLaying = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0176" domain="userortestcode"><source><![CDATA[View.SystemMessage("Play");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0187" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0188" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [PexMethod]
        public void Evolve() ]]></source>
</chunk>
<method name="Evolve" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f3" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="2"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="10" value="1"/>
<hit index="11" value="1"/>
<hit index="26" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="4" total="8" coveredRatio="0.5"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (TimeStamp == _caHistory.Count)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="001b" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ //we're not 'back in time'
                ]]></source>
</chunk>
<chunk hits="1" offset="001c" domain="userortestcode"><source><![CDATA[_caHistory.Add((CellularAutomaton)_ca.Clone());]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0038" domain="userortestcode"><source><![CDATA[_ca.Evolve();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0044" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="0045" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Make this CaViewController play it's Cellular Automaton. This includes updating registred views
        /// and sound.
        /// </summary>
        [PexMethod] 
        public void Play() ]]></source>
</chunk>
<method name="Play" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f4" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="10" value="2"/>
<hit index="11" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="4" total="18" coveredRatio="0.22222222222222221"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            //make sure we have a valid timestamp, could go wrong when going forward in time
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[validTimestamp = TimeStamp;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="000d" domain="userortestcode"><source><![CDATA[if (validTimestamp >= _caHistory.Count)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="0024" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0026" domain="userortestcode"><source><![CDATA[State[,] grid = Convertor.CAToStateGrid(_caHistory[validTimestamp]);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="003d" domain="userortestcode"><source><![CDATA[if (MainController.LaunchpadConnected && InputMode == InputMode.Launchpad)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0057" domain="userortestcode"><source><![CDATA[_hardWareView.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0064" domain="userortestcode"><source><![CDATA[_mo.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0071" domain="userortestcode"><source><![CDATA[View.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="007e" domain="userortestcode"><source><![CDATA[TimeStamp++;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="008d" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Pause was clicked. Clicking pause twice works as a kind of reset of the CA and views.
        /// </summary>
        [PexMethod]         
        public void Pause() ]]></source>
</chunk>
<method name="Pause" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f5" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="39" value="1"/>
<hit index="40" value="1"/>
<hit index="41" value="1"/>
<hit index="42" value="1"/>
<hit index="43" value="1"/>
<hit index="47" value="1"/>
<hit index="48" value="1"/>
<hit index="50" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="7" total="20" coveredRatio="0.35"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (IsPLaying)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="000e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="000f" domain="userortestcode"><source><![CDATA[IsPLaying = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0017" domain="userortestcode"><source><![CDATA[_caReadyForReplay = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="001e" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="0021" domain="userortestcode"><source><![CDATA[if (IsPaused)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="002e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ //if pause is clicked twice
                ]]></source>
</chunk>
<chunk hits="0" offset="002f" domain="userortestcode"><source><![CDATA[IsPLaying = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0037" domain="userortestcode"><source><![CDATA[ResetGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="003e" domain="userortestcode"><source><![CDATA[_caHistory.Clear();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="004a" domain="userortestcode"><source><![CDATA[TimeStamp = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0052" domain="userortestcode"><source><![CDATA[validTimestamp = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0059" domain="userortestcode"><source><![CDATA[_caReadyForReplay = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0060" domain="userortestcode"><source><![CDATA[if (_hardWareView != null)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="0" offset="006d" domain="userortestcode"><source><![CDATA[_hardWareView.ClearGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0079" domain="userortestcode"><source><![CDATA[View.SystemMessage("CA has been reset");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="008a" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="008b" domain="userortestcode"><source><![CDATA[IsPaused = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0093" domain="userortestcode"><source><![CDATA[View.SystemMessage("Pause");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="00a4" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        //Go forward in time!
        [PexMethod]
        public void Forward() ]]></source>
</chunk>
<method name="Forward" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f6" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="2"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="18" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="4" total="9" coveredRatio="0.44444444444444442"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (TimeStamp == _caHistory.Count)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="001b" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="001c" domain="userortestcode"><source><![CDATA[Evolve();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0023" domain="userortestcode"><source><![CDATA[Play();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="002a" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="0" offset="002d" domain="userortestcode"><source><![CDATA[TimeStamp++;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="003c" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        //rewind
        [PexMethod]
        public void Rewind() ]]></source>
</chunk>
<method name="Rewind" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f7" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="42" value="1"/>
<hit index="43" value="1"/>
<hit index="44" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="6" total="23" coveredRatio="0.2608695652173913"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (0 <= TimeStamp - 3)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0010" domain="userortestcode"><source><![CDATA[TimeStamp -= 3;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="001f" domain="userortestcode"><source><![CDATA[if (IsPaused && _caReadyForReplay)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0037" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0038" domain="userortestcode"><source><![CDATA[State[,] grid = Convertor.CAToStateGrid(_caHistory[TimeStamp]);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="004f" domain="userortestcode"><source><![CDATA[if (MainController.LaunchpadConnected && InputMode == InputMode.Launchpad)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="0" offset="0069" domain="userortestcode"><source><![CDATA[_hardWareView.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0076" domain="userortestcode"><source><![CDATA[_mo.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0083" domain="userortestcode"><source><![CDATA[View.SetGrid(grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0090" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0091" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[


        /// <summary>
        /// Save the current instance of of the ca along relevant history and state for later replication.
        /// </summary>
        /// <param name="name"></param>
        [PexMethod]         
        public void Save(String name) ]]></source>
</chunk>
<method name="Save" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f8" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="57" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="3" total="18" coveredRatio="0.16666666666666666"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(name != null);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_ca == null)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="0011" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="0012" domain="userortestcode"><source><![CDATA[View.SystemMessage("No CA to save.");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0023" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            }
            ]]></source>
</chunk>
<chunk hits="0" offset="0028" domain="userortestcode"><source><![CDATA[CaSaveContainer cas = new CaSaveContainer() {
                CellularAutomaton = _ca,
                CaHistory = _caHistory,
                ChosenChannel = _chosenChannel,
                Volume = this.Volume,
                Grid = new Size() { Width = _grid.GetLength(0), Height = _grid.GetLength(1) }
            };]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="009c" domain="userortestcode"><source><![CDATA[SaveCa.Save(cas, name);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="00a4" domain="userortestcode"><source><![CDATA[View.SystemMessage("Cellular Automaton saved as " + name);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="00bb" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Load and and play the automaton that is found
        /// </summary>
        /// <param name="path"></param>
        [PexMethod]
        public void Load(String path) ]]></source>
</chunk>
<method name="Load" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000f9" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="82" value="1"/>
</hits>
<tags><tag offset="0002">call to uninstrumented method File.Exists(String)</tag>
</tags>
<coverage domain="userortestcode" unit="block" covered="3" total="27" coveredRatio="0.11111111111111111"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(path.Length > 0);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode" tag="true"><tooltip><![CDATA[call to uninstrumented method File.Exists(String)]]></tooltip>
<source><![CDATA[if (File.Exists(path))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0011" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0012" domain="userortestcode"><source><![CDATA[CaSaveContainer csc = LoadCA.Load(path);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0019" domain="userortestcode"><source><![CDATA[if (csc == null)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0024" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="0" offset="0025" domain="userortestcode"><source><![CDATA[View.SystemMessage("CA at: " + path + " not found");]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="0" offset="0041" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                }
                ]]></source>
</chunk>
<chunk hits="0" offset="0046" domain="userortestcode"><source><![CDATA[this._ca = csc.CellularAutomaton;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0052" domain="userortestcode"><source><![CDATA[this._caHistory = csc.CaHistory;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="005e" domain="userortestcode"><source><![CDATA[this.Volume = csc.Volume;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="006b" domain="userortestcode"><source><![CDATA[this._chosenChannel = csc.ChosenChannel;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0077" domain="userortestcode"><source><![CDATA[this._grid = new State[csc.Grid.Width, csc.Grid.Height];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="009e" domain="userortestcode"><source><![CDATA[ResetGrid();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00a5" domain="userortestcode"><source><![CDATA[_userIsSettingCARule = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00ac" domain="userortestcode"><source><![CDATA[_userIsSettingCAInitialState = false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00b3" domain="userortestcode"><source><![CDATA[InputMode = InputMode.Off;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

                ]]></source>
</chunk>
<chunk hits="0" offset="00bb" domain="userortestcode"><source><![CDATA[View.DisplayChannel(this._chosenChannel);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00cd" domain="userortestcode"><source><![CDATA[View.SetVolume(Volume);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="00df" domain="userortestcode"><source><![CDATA[IsPLaying = true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="00e7" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="00e8" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [PexMethod]
        public void SetVolume(int volume) ]]></source>
</chunk>
<method name="SetVolume" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000fa" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="2"/>
<hit index="10" value="1"/>
<hit index="11" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="7" total="7" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Ensures(0 <= Volume && Volume <= 100);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (0 <= volume && volume <= 100)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="0011" domain="userortestcode"><source><![CDATA[this.Volume = volume;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0019" domain="userortestcode"><source><![CDATA[_mo.SetVolume(volume);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="3" offset="0026" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Sets the channel that is chosen for sound output.
        /// </summary>
        [PexMethod] 
        public void SetChannel(Channel c) ]]></source>
</chunk>
<method name="SetChannel" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000fb" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="2"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="2" total="2" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Ensures(_chosenChannel.Equals(c));
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_chosenChannel = c;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[_mo.SetChannel(c);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="3" offset="0015" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Updates a grid. The controller always knows what cell there's being tampered with.
        /// </summary>
        /// <param name="grid">The grid that needs to be updated</param>
        [PexMethod]        
        private void SetGridCell(State[,] grid) ]]></source>
</chunk>
<method name="SetGridCell" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000fc" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="17" value="1"/>
<hit index="18" value="1"/>
<hit index="19" value="1"/>
<hit index="20" value="1"/>
<hit index="21" value="1"/>
<hit index="22" value="1"/>
<hit index="23" value="1"/>
<hit index="24" value="1"/>
<hit index="25" value="1"/>
<hit index="26" value="1"/>
<hit index="27" value="1"/>
<hit index="28" value="1"/>
<hit index="29" value="1"/>
<hit index="30" value="2"/>
<hit index="31" value="1"/>
<hit index="32" value="1"/>
<hit index="33" value="1"/>
<hit index="34" value="2"/>
<hit index="35" value="1"/>
<hit index="36" value="1"/>
<hit index="37" value="1"/>
<hit index="38" value="1"/>
<hit index="39" value="1"/>
<hit index="40" value="2"/>
<hit index="41" value="1"/>
<hit index="42" value="1"/>
<hit index="43" value="1"/>
<hit index="44" value="1"/>
<hit index="71" value="1"/>
<hit index="72" value="1"/>
<hit index="73" value="1"/>
<hit index="74" value="1"/>
<hit index="75" value="1"/>
<hit index="76" value="1"/>
<hit index="77" value="1"/>
<hit index="78" value="1"/>
<hit index="79" value="1"/>
<hit index="80" value="1"/>
<hit index="93" value="1"/>
<hit index="95" value="1"/>
<hit index="96" value="1"/>
<hit index="97" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="9" total="18" coveredRatio="0.5"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(grid != null);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_stateBeingChanged)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="000e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="000f" domain="userortestcode"><source><![CDATA[grid[_x, _y].CurrentState = _state.CurrentState;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0032" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ else ]]></source>
</chunk>
<chunk hits="1" offset="0035" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="0036" domain="userortestcode"><source><![CDATA[grid[_x, _y].CurrentState = ++grid[_x, _y].CurrentState % NUMBER_OF_STATES;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="3" offset="0073" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="3" offset="0074" domain="userortestcode"><source><![CDATA[if (IsPLaying || (_caReadyForReplay))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="008c" domain="userortestcode"><source><![CDATA[_ca[_x, _y] = grid[_x, _y].CurrentState;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="00c9" domain="userortestcode"><source><![CDATA[View.SetCell(_x, _y, grid[_x, _y]);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="00f3" domain="userortestcode"><source><![CDATA[NotifyLaunchpadOnCellClick(_x, _y, grid[_x, _y]);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="0118" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// A cell was clicked. 
        /// </summary>
        /// <param name="xPos"></param>
        /// <param name="yPos"></param>
        [PexMethod]        
        private void CellClicked(int xPos, int yPos) ]]></source>
</chunk>
<method name="CellClicked" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000fd" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="10" value="1"/>
<hit index="21" value="1"/>
<hit index="22" value="1"/>
<hit index="23" value="1"/>
<hit index="24" value="1"/>
<hit index="25" value="1"/>
<hit index="26" value="1"/>
<hit index="27" value="1"/>
<hit index="28" value="1"/>
<hit index="29" value="1"/>
<hit index="30" value="1"/>
<hit index="31" value="1"/>
<hit index="33" value="1"/>
<hit index="34" value="1"/>
<hit index="35" value="1"/>
<hit index="37" value="1"/>
<hit index="38" value="1"/>
<hit index="39" value="1"/>
<hit index="40" value="1"/>
<hit index="41" value="1"/>
<hit index="42" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="11" total="19" coveredRatio="0.57894736842105265"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(_grid != null);
            Contract.Requires(_rule != null);
            Contract.Requires(0 <= _x && 0 <= _y);
            //Only affect cells if user is in a set cell kind of mode that is also valid.);););
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_userIsSettingCARule && !CellIsValid(xPos, yPos))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0018" domain="userortestcode"><source><![CDATA[return;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="001d" domain="userortestcode"><source><![CDATA[_x = xPos;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0024" domain="userortestcode"><source><![CDATA[_y = yPos;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="002b" domain="userortestcode"><source><![CDATA[if (_userIsSettingCARule && xPos < _rule.GetLength(0) && yPos < _rule.GetLength(1))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="005b" domain="userortestcode"><source><![CDATA[SetGridCell(_rule);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            else ]]></source>
</chunk>
<chunk hits="1" offset="006a" domain="userortestcode"><source><![CDATA[if ((xPos < _grid.GetLength(0) && yPos < _grid.GetLength(1)) && CellIsValid(xPos, yPos))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="1" offset="009a" domain="userortestcode"><source><![CDATA[SetGridCell(_grid);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="00a7" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Checks if it is ok to use the Launchpad as input
        /// </summary>
        /// <returns></returns>
        private bool OkToUseLaunchpad() ]]></source>
</chunk>
<method name="OkToUseLaunchpad" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="60000fe" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="8" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="3" total="5" coveredRatio="0.6"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[return (_userIsSettingCAInitialState || _userIsSettingCARule) && InputMode == InputMode.Launchpad;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0020" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        private void NotifyLaunchpadOnCellClick(int x, int y, State state) {
            if (InputMode == InputMode.Launchpad && _hardWareView != null) {
                _hardWareView.SetCell(x, y, state);
            }
        }

        private void SaveRule() {
            rules.Add(_rule);
            View.SystemMessage("Set rule # " + (rules.Count + 1) + " Press ok to enter more rules. Press Play to listen.");
        }

        /// <summary>
        /// Check to see if a clicked cell is valid according to the rule selected. At this point the user has a grid of 3 x 3
        /// </summary>
        /// <returns></returns>
        private Boolean CellIsValid(int xPos, int yPos) ]]></source>
</chunk>
<method name="CellIsValid" namespace="AutomaTones.Controller" type="AutomaTones.Controller.CAViewController" typeName="CAViewController" token="6000101" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="6" value="1"/>
<hit index="14" value="1"/>
<hit index="15" value="1"/>
<hit index="19" value="1"/>
<hit index="20" value="1"/>
<hit index="21" value="1"/>
<hit index="23" value="1"/>
<hit index="24" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="7" total="19" coveredRatio="0.36842105263157893"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[if (_userIsSettingCAInitialState && ChosenCAType == CAType.OneDimensional)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="001c" domain="userortestcode"><source><![CDATA[return yPos == 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0023" domain="userortestcode"><source><![CDATA[if (_userIsSettingCARule && (xPos >= _rule.GetLength(0) || yPos >= _rule.GetLength(0)))]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0053" domain="userortestcode"><source><![CDATA[return false;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0057" domain="userortestcode"><source><![CDATA[if (ChosenCAType == CAType.OneDimensional)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="0067" domain="userortestcode"><source><![CDATA[return yPos == 0 || (yPos == 1 && xPos == 1);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="007b" domain="userortestcode"><source><![CDATA[return true;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="007f" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Change the CAType.
        /// </summary>
        /// <param name="type"></param>
        private void ChangeCaType(CAType type) {
            //Dimension changed, and we don't combine these so clear the listed rules in this case and go to config of ca
            if (type != ChosenCAType && type == CAType.TwoDimensional || type == CAType.OneDimensional) {
                rules.Clear();
                View.SystemMessage("Dimensions can't be combined...");
                _userIsSettingCARule = false;
                _userIsSettingCAInitialState = true;
                this.ChosenCAType = type;
                ResetGrid();
                return;
            }
            this.ChosenCAType = type;
            View.SystemMessage("" + type);
            if (_userIsSettingCARule)
                ResetRuleGrid();
        }

        /// <summary>
        /// Called when a new input mode is selected. For the Launchpad we start using the 8 x 8
        /// already held in this class. For mouse configuration of CA we use the preferred grid used
        /// by the IVIEW
        /// </summary>
        /// <param name="im"></param>
        private void ChangeInputMode(InputMode im) {
            if (IsPLaying) {
                View.SystemMessage("Stop music to modify CA input mode");
                return;
            }
            InputMode = im;
            View.SystemMessage("Input mode: " + this.InputMode);
            switch (im) {
                case InputMode.Launchpad: {
                        _grid = new State[LAUNCHPAD_DIMENSION, LAUNCHPAD_DIMENSION];
                        _userIsSettingCAInitialState = true;
                    }
                    break;
                case InputMode.Mouse: {
                        _grid = View.CustomGrid;
                        _userIsSettingCAInitialState = true;
                    }
                    break;
                case InputMode.Off: {
                        _userIsSettingCAInitialState = false;
                    }
                    break;
            }
            _userIsSettingCARule = false;
            ResetGrid();
            if (rules.Count > 0)
                rules.Clear();
        }

        /// <summary>
        /// Changes the mapper
        /// </summary>
        /// <param name="mapper"></param>
        private void ChangeMapper(Mapper mapper) {
            switch (mapper) {
                case Mapper.BechTranslator: {
                        _mo.SetMapper(new BechTranslator());
                        View.SystemMessage("" + mapper);
                    }
                    break;
                case Mapper.HqstTranslator: {
                        _mo.SetMapper(new HqstTranslator());
                         View.SystemMessage("" + mapper);
                    }
                    break;
                case Mapper.MozartTranslator:
                    {
                        _mo.SetMapper(new MozartTranslator());
                    }
                    break;
                case Mapper.SampleTranslator:
                    {
                        _mo.SetMapper(new SampleTranslator());
                    }
                    break;
                default: _mo.SetMapper(new BechTranslator());
                    break;
            }
        }

        /// <summary>
        /// Adds a pattern to the Cellular Automaton
        /// </summary>
        /// <param name="column"></param>
        /// <param name="row"></param>
        private void ApplyCaPattern(GameOfLife.GofPattern pattern, int column, int row) {
            if (this.InputMode == InputMode.Off)
                return;
            if (_ca == null) {
                if (ChosenCAType == CAType.GameOfLife) {
                    CaTypeSetup.SetupGameOfLife(ref _ca, _grid.GetLength(0), _grid.GetLength(1), 1, _grid);
                } else if (ChosenCAType == CAType.GameOfLifeEXT)
                    CaTypeSetup.SetupGameOfLife(ref _ca, _grid.GetLength(0), _grid.GetLength(1), NUMBER_OF_STATES, _grid);
                ChangeCaType(CAType.Off);
            }
            GameOfLife.AddGofPattern(_ca, pattern, column, row);
            IsPLaying = true;
            View.SystemMessage("Playing. " + pattern);
        }


        /// <summary>
        /// Reset the launchpad sized grid on the IView and the IHardWareView. If the ca type and config is one dimensional
        /// it add some state to the cells that are not clickable.
        /// </summary>
        public void ResetGrid() {
            for (int i = 0; i < _grid.GetLength(0); i++) {
                for (int j = 0; j < _grid.GetLength(1); j++) {
                    if (ChosenCAType == CAType.OneDimensional && !CellIsValid(i, j))
                    {
                        _grid[i, j] = new State(0, 8, 0);
                        continue;
                    }
                    _grid[i, j] = new State();
                }
            }
            if (_hardWareView != null)
                _hardWareView.ClearGrid();
            if (View != null) {
                View.SetGridDimension(_grid.GetLength(0), _grid.GetLength(1));
                View.SetGrid(_grid);
            }
        }

        /// <summary>
        /// Reset the rule grid on the IView and the IHardWareView. If the rule is one dimensional
        /// it add some state to the cells that are not clickable.
        /// </summary>
        private void ResetRuleGrid() {
            for (int i = 0; i < _rule.GetLength(0); i++)
                for (int j = 0; j < _rule.GetLength(1); j++) {
                    if (ChosenCAType == CAType.OneDimensional && !CellIsValid(i, j)) {
                        _rule[i, j] = new State(0, 8, 0);
                        continue;
                    }
                    _rule[i, j] = new State();
                }
            if (_hardWareView != null) {
                _hardWareView.ClearGrid();
                _hardWareView.SetGrid(_rule);
            }
            if (View != null) {
                View.SetGridDimension(_rule.GetLength(0), _rule.GetLength(1));
                View.SetGrid(_rule);
            }
        }

        private int TimeStamp { get; set; }
        private IView View { get; set; }
        private CAType ChosenCAType { get; set; }
        private InputMode InputMode { get; set; }

        /// <summary>
        /// Trivial.
        /// Setup this controller with the view that has just registred itself to
        /// this controller. It assigns methods to delegates and eventhandlers. 
        /// </summary>
        public void SetUp() {
            View.OnCellClickEvent += CellClicked;
            View.OnPlayButtonClickEvent += PlaySentFromUser;
            View.OnPauseButtonClickEvent += Pause;
            View.OnVolumeChangeEvent += SetVolume;
            View.OnChannelChangeEvent += SetChannel;
            View.OnOkButtonClickEvent += Ok;
            View.OnSaveButtonClickEvent += Save;
            View.OnLoadButtonClickEvent += Load;
            View.OnInputModeChangeEvent += ChangeInputMode;
            View.OnCATypeChangeEvent += ChangeCaType;
            View.OnMapperChangeEvent += ChangeMapper;
            View.OnRewindButtonClickEvent += Rewind;
            View.OnForwardButtonClickEvent += Forward;
            View.OnApplyCaPatternEvent += ApplyCaPattern;
            //Aval data
            View.Patterns = Model.Utils.AutomatoneTypes.GetPatterns();
            View.DisplayInputModes(Model.Utils.AutomatoneTypes.GetInputModes());
            View.DisplayCATypes(Model.Utils.AutomatoneTypes.GetCaTypes());
            View.DisplayMappers(Model.Utils.AutomatoneTypes.GetMappers());
            View.DisplayChannels(Channels);
        }

        [ContractInvariantMethod]
        private void Invariant() {
            Contract.Invariant(validTimestamp <= _caHistory.Count);
            Contract.Invariant(0 <= this.Volume && this.Volume <= 100);
            //if chosen CA type changes, no old rules should be kept.
            Contract.Invariant(Contract.OldValue(ChosenCAType) != ChosenCAType && rules.Count == 0 && _userIsSettingCARule);
            Contract.Invariant((_rule != null));
            Contract.Invariant(_grid != null);
            Contract.Invariant(rules != null);
            Contract.Invariant(InputMode == InputMode.Launchpad && _hardWareView != null);
            Contract.Invariant(0 <= TimeStamp);
            Contract.Invariant(_caReadyForReplay && _ca != null);
            Contract.Invariant(IsPLaying && _ca != null);
        }
    }
}
]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\State.cs" renderPrefix="s1"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Diagnostics.Contracts;

namespace AutomaTones.Model.Automaton_Model {
    /// <author>Anders Øland / anoe@itu.dk</author>
    /// <summary>
    /// The state of a cell represented by a discrete value within a given state space.. 
    /// NOTE: Ideally this would be implemented using generics so states could also be decimals.
    /// NOTE: But due to C#'s lack of type constraint options on value types - this was omitted.
    /// </summary>
    [Serializable]
    public class State {
        /// <summary>
        /// The lower boundary of the state space.
        /// </summary>
        public int SpaceMin { get; protected set; }

        /// <summary>
        /// The upper boundary of the state space.
        /// </summary>
        public int SpaceMax { get; protected set; }

        private int _currentState;

        /// <summary>
        /// Current state.
        /// </summary>
        public int CurrentState {
            get ]]></source>
</chunk>
<method name="get_CurrentState" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.State" typeName="State" token="60001ce" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[return _currentState;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="1" offset="000a" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            protected internal set ]]></source>
</chunk>
<method name="set_CurrentState" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.State" typeName="State" token="60001cf" module="AutomaTones.exe"><hits><hit index="0" value="2"/>
<hit index="1" value="2"/>
<hit index="2" value="2"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="2" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                Contract.Ensures(CurrentState == value);
                ]]></source>
</chunk>
<chunk hits="2" offset="0001" domain="userortestcode"><source><![CDATA[_currentState = value;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="4" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        }

        private int _nextState;

        /// <summary>
        /// Next state.
        /// </summary>
        public int NextState {
            get { return _nextState; }
            protected internal set {
                Contract.Ensures(NextState == value);
                _nextState = value;
            }
        }

        /// <summary>
        /// Construct a new State. 
        /// </summary>
        /// <param name="spaceMin"></param>
        /// <param name="spaceMax"></param>
        /// <param name="currentState"></param>
        public State(int spaceMin = 0, int spaceMax = 1, int currentState = 0) {
            SpaceMin = spaceMin;
            SpaceMax = spaceMax;
            _currentState = currentState;
        }

        [ContractInvariantMethod]
        private void ObjInvariant() {
            Contract.Invariant(SpaceMin < SpaceMax);
            Contract.Invariant(SpaceMin <= _currentState && SpaceMax >= _currentState);
            Contract.Invariant(SpaceMin <= _nextState && SpaceMax >= _nextState);
        }
    }
}]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\View\Midi Out View\MidiOut.cs" renderPrefix="s2"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using AutomaTones.HW_Sense_Compute;
using AutomaTones.Model.Automaton_Model;
using AutomaTones.Model.MIDI;
using Midi;
using Microsoft.Pex.Framework;

namespace AutomaTones.View.Midi_Out_View {
    /// <summary>
    /// MidiOut is holding a reference to the virtual midi driver, LoopBe.
    /// Which lets the program communicate with other programs through standard MIDI.
    /// </summary>
    /// <author>Anders Bech Mellson, anbh@itu.dk</author>
    [PexClass]
    class MidiOut : IHardwareView {
        private Channel _channel;
        private IMapper _mapper;
        private int _volume;

        /// <summary>
        /// MidiOut constructor, that initialises key components for use.
        /// Also the constructor opens the Midi output, if it is not already open.
        /// </summary>
        /// <param name="channel">The Midi channel the MidiOut will send messages on.</param>
        /// <param name="mapper">The mapper that MidiOut will use to translate State[,] grid's into Midi notes.</param>
        public MidiOut(Channel channel, IMapper mapper) {
            Contract.Requires(mapper != null);
            _channel = channel;
            _mapper = mapper;
            if (!ConnectVirtualMidi.Out.IsOpen)
                ConnectVirtualMidi.Out.Open();
            _volume = 100;
        }

        /// <summary>
        /// Transforms a State[,] grid into Midi notes that is sent to the connected output.
        /// The transformation is being calculated by the mapper.
        /// </summary>
        /// <param name="stateGrid">Grid to be played by MidiOut.</param>
        [PexMethod]
        public void SetGrid(State[,] stateGrid) {
            //Contract.Requires(stateGrid!=null);
            List<NoteOut> notesToPlay = _mapper.Translate(stateGrid);
            foreach (NoteOut note in notesToPlay) {
                ConnectVirtualMidi.Out.SendNoteOn(_channel,note.Pitch,_volume*note.Velocity/100);
                if (note.Controller != 0)
                    ConnectVirtualMidi.Out.SendControlChange(_channel, Control.Volume + note.Controller,
                                                             note.ControllerValue);
            }
        }

        /// <summary>
        /// Silences all the notes being played by this MidiOut.
        /// </summary>
        [PexMethod]
        public void ClearGrid() {
            ConnectVirtualMidi.Out.SilenceAllNotes();
        }

        /// <summary>
        /// Change the MidiOutput channel.
        /// </summary>
        /// <param name="channel">The Channel that the MidiOut should send on</param>
        [PexMethod]
        public void SetChannel(Channel channel) ]]></source>
</chunk>
<method name="SetChannel" namespace="AutomaTones.View.Midi_Out_View" type="AutomaTones.View.Midi_Out_View.MidiOut" typeName="MidiOut" token="60000c7" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_channel = channel;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Change the Midi out volume relatively compared to the velocity of each note.
        /// If you send 100% then the notes will play at their normal velocity,
        /// sending lower volumes will turn each notes velocity down.
        /// </summary>
        /// <param name="volume">Volume in %.</param>
        [PexMethod]
        public void SetVolume(int volume) ]]></source>
</chunk>
<method name="SetVolume" namespace="AutomaTones.View.Midi_Out_View" type="AutomaTones.View.Midi_Out_View.MidiOut" typeName="MidiOut" token="60000c8" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(volume<=100 && volume >=0);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[_volume = volume;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="2" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// Change the mapper on the MidiOut. The mapper is in charge of the way that
        /// Cellular Automatones gets translated into Midi notes.
        /// Have fun with trying different mappers.
        /// </summary>
        /// <param name="mapper">The new mapper you wish to assign to this MidiOut.</param>
        [PexMethod]
        public void SetMapper(IMapper mapper) {
            Contract.Requires(mapper != null);
            _mapper = mapper;
        }


        /// <summary>
        /// Inherited method from IHardwareView.
        /// Could convert one state to a MidiNote,
        /// but it wasn't needed in the end as our design changed.
        /// </summary>
        [PexMethod]
        public void SetCell(int x, int y, State state)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Inherited method from IHardwareView.
        /// Not implemented here, because I couldn't figure out a use for it.
        /// </summary>
        [PexMethod]
        public void SetArbitraryGrid(State[,] stateGrid)
        {
            throw new NotImplementedException();
        }

        [ContractInvariantMethod]
        private void Invariant() {
            Contract.Invariant(_mapper!=null);
        }
    }
}
]]></source>
</chunk>
</document>
<document src="(no sources available for some parts of AutomaTones)" missing="true"><method name="set_View" type="AutomaTones.Controller.CAViewController" token="600010b" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="get_ChosenCAType" type="AutomaTones.Controller.CAViewController" token="600010c" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="get_IsPLaying" type="AutomaTones.Controller.CAViewController" token="60000e4" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="get_View" type="AutomaTones.Controller.CAViewController" token="600010a" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="get_TimeStamp" type="AutomaTones.Controller.CAViewController" token="6000108" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="get_IsPaused" type="AutomaTones.Controller.CAViewController" token="60000e6" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="set_IsPaused" type="AutomaTones.Controller.CAViewController" token="60000e7" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="set_Volume" type="AutomaTones.Controller.CAViewController" token="60000e9" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
</document>
</documents>
